package com.renfei.multimediatask.ui.task9.prefect;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.annotation.RequiresApi;

import com.renfei.multimediatask.R;
import com.renfei.multimediatask.base.BaseActivity;
import com.renfei.multimediatask.ui.task9.AutoFitTextureView;
import com.renfei.multimediatask.ui.task9.encodec.MediaAudioEncoder;
import com.renfei.multimediatask.ui.task9.encodec.MediaManager;
import com.renfei.multimediatask.ui.task9.encodec.MediaVideoEncoder;
import com.renfei.multimediatask.util.YUVDataUtil;


import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;

import butterknife.BindView;
import butterknife.OnClick;

public class VideoRecordAllProcessPrefect extends BaseActivity {
    private static final boolean DEBUG = true;    // TODO set false on release
    public static final String TAG = "VideoRecordPrefect";
    public String targetFilePath;

    @BindView(R.id.texture)
    AutoFitTextureView texture;
    @BindView(R.id.startRecord)
    Button startRecord;
    @BindView(R.id.stopRecord)
    Button stopRecord;
    @BindView(R.id.ll_btn_layout)
    RelativeLayout llBtnLayout;


    private CameraHelperPrefect cameraHelper;
    private int logCount = 0;
    public ArrayBlockingQueue<byte[]> queue;


    private int recordWidth;
    private int recordHeight;


    private MediaManager mediaManager;


    private boolean isStart = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_record_all_process;
    }

    @Override
    protected void initListener() {

        targetFilePath = getParentFilePath() + "test_create.mp4";

        queue = new ArrayBlockingQueue<>(10);

        cameraHelper = new CameraHelperPrefect(new WeakReference<>(this), texture);
        cameraHelper.setOnPreviewFrameListener(new CameraHelperPrefect.OnPreviewFrameListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onPreviewFrame(byte[] data, int width, int height) {
                if (logCount < 1) {

                    recordWidth = width;
                    recordHeight = height;

                    loge(TAG, "onPreviewFrame    " + data.length
                            + "     width   " + width
                            + "     height   " + height
                    );
                    logCount++;
                }
                if (null != queue && isStart) {
                    try {
                        if (queue.size() >= 9) {
                            queue.poll();
                        } else {
                            queue.put(data);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onResume() {
        super.onResume();
        cameraHelper.onResume();
    }

    @Override
    protected void onPause() {
        stopRecording_1();
        cameraHelper.onPause();
        super.onPause();
    }

    @OnClick({R.id.startRecord, R.id.stopRecord})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.startRecord:
                if (null == mediaManager)
                    startRecording_1();
                break;
            case R.id.stopRecord:
                stopRecording_1();
                break;
        }
    }


    /**
     * 用来测试的
     */

    private void startRecording_1() {
        try {
            mediaManager = new MediaManager();


            new MediaVideoEncoder(mediaManager, new WeakReference<>(this), recordWidth, recordHeight);
            new MediaAudioEncoder(mediaManager);

            isStart = true;

            mediaManager.prepare();
            mediaManager.startRecording();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stopRecording_1() {
        if (isStart) {
            isStart = false;
            if (null != mediaManager) {
                mediaManager.stopRecording();
            }
        }

    }
}
